package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.vo.CurCourseBaseResp;
import com.macro.mall.model.CurCourseBase;
import com.macro.mall.portal.service.CurCourseBaseService;
import com.macro.mall.portal.service.MbrMemberCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * 课程基础表(cur_course_base）
 *
 * @author gaodx
 * @create 2020-08-11 15:43
 **/
@Slf4j
@RestController
@RequestMapping("curCourseBase")
@Api(tags = "CurCourseBaseController", description = "课程基础表(cur_course_base）")
public class CurCourseBaseController {


    @Autowired
    private CurCourseBaseService curCourseBaseService;
    @Autowired
    private MbrMemberCollectService memberCollectService;



    @ApiOperation("查询课程")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CurCourseBaseResp>> list(@RequestParam(value = "searchName", required = false) String searchName,
                                                            @RequestParam(value = "courseType", required = false) Integer courseType,
                                                            @ApiParam(value = "ccb.create_time desc, t.play_count desc,  ccb.update_time desc")@RequestParam(value = "orderBy", required = false) String orderBy,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<CurCourseBaseResp> bycondition = curCourseBaseService.findBycondition(searchName, courseType, orderBy, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(bycondition));
    }



    @ApiOperation("查询课程详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CurCourseBaseResp> getById(Principal principal, @ApiParam(value = "id") @RequestParam(value = "id", required = true) Long id) {
        try {
            if(principal==null){
                return CommonResult.unauthorized(null);
            }
            CurCourseBaseResp one = curCourseBaseService.findOne(id);
            CommonResult<CurCourseBaseResp> success = CommonResult.success(one);
            return success;
        } catch (Exception e) {
            log.error("exception", e);
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询用户收藏课程")
    @RequestMapping(value = "/collectList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<CurCourseBase>> list(Principal principal, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        List<CurCourseBase> curCourseBase = curCourseBaseService.findMbrMemberCollect(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(curCourseBase));
    }

    @ApiOperation("加入收藏")
    @RequestMapping(value = "/collectSave", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult collectSave(Principal principal, @RequestParam(value = "courseId", defaultValue = "课程标识") Integer courseId) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        memberCollectService.saveNotNull(courseId);
        return CommonResult.success(null);
    }
    @ApiOperation("取消收藏")
    @RequestMapping(value = "/collectDel", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult collectDel(Principal principal,@RequestParam(value = "courseId", defaultValue = "课程标识") Integer courseId) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        memberCollectService.deleteByMbrMemberCollect(courseId);
        return CommonResult.success(null);
    }

















}